<?php
/**
 * User:于小涛
 * Date:2020/2/16
 * Time:17:12
 */
namespace app\admin\model\rbac;
use think\Model;

class Admin extends Model{
    protected $name='rbac_admin';

    public function getAdminInfoByAccountPwd($str_account,$str_pwd){
        return $this->field("id,name,token,type,is_lock,password")
            ->where('account', $str_account)
            ->where('password',md5($str_pwd.$str_pwd))
            ->findOrEmpty()
            ->toArray();
    }

    public function updateLoginData($id){
        $str_date_time=date("Y-m-d H:i:s");
        $res_admin=$this->find($id);
        $res_admin->last_login_time=$str_date_time;
        $res_admin->modify_time=$str_date_time;
        $res_admin->last_login_ip=get_client_ip();
        return $res_admin->save();
    }

    public function getList($arr_where,$int_page_num,$int_page_size,$arr_order=array('create_time'=>'desc')){
        return $this->field("id,name,account,type,is_lock,last_login_time,last_login_ip,create_time,modify_time")
            ->page($int_page_num,$int_page_size)
            ->where($arr_where)
            ->order($arr_order)
            ->paginate(['list_rows'=>$int_page_size,'page'=>$int_page_num])
            ->toArray();
    }

    public function add($arr_data){
        $str_time=date("Y-m-d H:i:s");
        $arr_data['create_time'] = $str_time;
        $arr_data['modify_time'] = $str_time;
        return $this->insert($arr_data);
    }

    public function edit($int_id,$arr_edit){
        $str_time=date("Y-m-d H:i:s");
        $arr_edit['modify_time'] = $str_time;
        return $this->where('id',$int_id)->update($arr_edit);
    }
}